﻿/*
    Copyright Multi-Soft co.2009 
*/

using System;
using System.Net;

using Rmlife.Common;

namespace Rmlife.Config
{
    /// <summary>
    /// 基本设置类
    /// </summary>
    public class BaseConfigs
    {
        private static System.Timers.Timer baseConfigTimer = new System.Timers.Timer(15000);

        private static BaseConfigInfo m_configinfo;

        /// <summary>
        /// 静态构造函数初始化相应实例和定时器
        /// </summary>
        static BaseConfigs()
        {
            m_configinfo = LoadConfig();
            baseConfigTimer.AutoReset = true;
            baseConfigTimer.Enabled = true;
            baseConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
            baseConfigTimer.Start();
        }

        /// <summary>
        /// 加载配置类
        /// </summary>
        /// <returns></returns>
        protected static BaseConfigInfo LoadConfig()
        {
            return LoadConfig(true);
        }
        protected static BaseConfigInfo LoadConfig(bool checkTime)
        {
            string regkey = CompanyProperties._RegPath;

            string Source = RegistryUtils.ReadString(regkey, "ServerIP");
            string UID = RegistryUtils.ReadString(regkey, "UID");
            string Password = RegistryUtils.ReadString(regkey, "Password");
            string Catalog = RegistryUtils.ReadString(regkey, "Database");
            string Encrypt = RegistryUtils.ReadString(regkey, "Encrypt", "0");

            if (Encrypt == "1")
            {
                //解密密码
                Password = DES.Decode(Password, CompanyProperties.ASSEMBLY_AUTHKEY);
            }

            string connStr = "Data Source=" + Source + ";User ID=" + UID + ";Password=" + Password + ";Initial Catalog=" + Catalog + ";Pooling=true";

            if (string.IsNullOrEmpty(UID))
            {
                // WINDOWS 身份验证串
                connStr = "Data Source=" + Source + ";Initial Catalog=" + Catalog + ";Integrated Security=True";
            }

            BaseConfigInfo info = new BaseConfigInfo();
            info.Dbconnectstring = connStr;
            info.Dbtype = RegistryUtils.ReadString(regkey, "Dbtype", "SqlServer");
            info.Founderuid = Utils.StrToInt(RegistryUtils.ReadString(regkey, "Founderuid", "1"), 1);
            info.Sitepath = RegistryUtils.ReadString(regkey, "Sitepath", "/");
            info.Tableprefix = RegistryUtils.ReadString(regkey, "Tableprefix", "");
            info.Proxy = RegistryUtils.ReadString(regkey, "Proxy", "");

            info.Version = RegistryUtils.ReadString(regkey, "Version", "1.0.0.0");
            info.VersionBuild = RegistryUtils.ReadString(regkey, "Build", "");

            return info;
        }

        /// <summary>
        /// 加载实时的配置类
        /// </summary>
        /// <returns></returns>
        protected static BaseConfigInfo LoadRealConfig()
        {
            m_configinfo = LoadConfig(false);
            return m_configinfo;
        }

        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ResetConfig();
        }
        /// <summary>
        /// 重设配置类实例
        /// </summary>
        public static void ResetConfig()
        {
            m_configinfo = LoadRealConfig();
        }

        public static BaseConfigInfo GetBaseConfig()
        {
            return m_configinfo;
        }

        /// <summary>
        /// 返回数据库连接串
        /// </summary>
        public static string GetDBConnectString
        {
            get
            {
                return GetBaseConfig().Dbconnectstring;
            }
        }
        /// <summary>
        /// 返回表前缀
        /// </summary>
        public static string GetTablePrefix
        {
            get
            {
                return GetBaseConfig().Tableprefix;
            }
        }

        //得到超级管理员ID
        public static int GetFounderUid
        {
            get
            {
                return GetBaseConfig().Founderuid;
            }
        }

        /// <summary>
        /// 返回路径
        /// </summary>
        public static string GetSitePath
        {
            get
            {
                return GetBaseConfig().Sitepath;
            }
        }

        /// <summary>
        /// 返回数据库类型
        /// </summary>
        public static string GetDbType
        {
            get
            {
                return GetBaseConfig().Dbtype;
            }
        }
        /// <summary>
        /// 保存配置实例
        /// </summary>
        /// <param name="baseconfiginfo"></param>
        /// <returns></returns>
        public static bool SaveConfig(BaseConfigInfo baseconfiginfo)
        {
            return true;
        }

        /// <summary>
        /// 返回代理服务器地址 IP,PORT
        /// </summary>
        public static string GetProxyUrl
        {
            get
            {
                return GetBaseConfig().Proxy;
            }
        }

        /// <summary>
        /// 返回代理服务器：格式为IP,PORT
        /// </summary>
        public static WebProxy GetProxy
        {
            get
            {
                string proxy = GetBaseConfig().Proxy;
                if (!string.IsNullOrEmpty(proxy))
                {
                    string server = proxy.Split(',')[0];
                    int port = proxy.Split(',').Length > 1 ? Utils.StrToInt(proxy.Split(',')[1], 8080) : 8080;
                    string username = proxy.Split(',').Length > 2 ? proxy.Split(',')[2] : string.Empty;
                    string password = proxy.Split(',').Length > 3 ? proxy.Split(',')[3] : string.Empty;
                    return Utils.SetProxy(server, port, username, password);
                }
                return null;
            }
        }
        /// <summary>
        /// 返回升级服务器地址
        /// </summary>
        public static string GerUpdateServer
        {
            get
            {
                return GetBaseConfig().UpdateServer;
            }
        }
        /// <summary>
        /// 获取版本号
        /// </summary>
        public static string GetVersion
        {
            get
            {
                return string.Format("{0}", GetBaseConfig().Version);
            }
        }
        /// <summary>
        /// 获取完整版本号
        /// </summary>
        public static string GetFullVersion
        {
            get
            {
                return string.Format("{0}.{1}", GetBaseConfig().Version, GetBaseConfig().VersionBuild);
            }
        }
    }
}
